package com.addressbook.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.addressbook.fw.ContactData;
import com.addressbook.fw.GroupData;
import com.addressbook.utils.SortedListOf;
import static com.addressbook.tests.ContactDataGenerator.loadsContactsFromXmlFile;
//import static com.addressbook.tests.ContactDataGenerator.loadsContactsFromCsvFile;

public class ContactCreationTests extends TestBase {

	@DataProvider
    public static Iterator<Object[]> getValidContactFromFile() throws IOException {
            return wrapContactsForDataProvider(
            		loadsContactsFromXmlFile(new File("contacts.xml"))).iterator();
//            		loadsContactsFromCsvFile(new File("contacts.txt"))).iterator();
    }
	
	@Test(dataProvider = "getValidContactFromFile")
	public void testContactCreationValidData(ContactData contact) throws Exception {
		// save old
		SortedListOf<ContactData> oldstate = new SortedListOf<ContactData> (app.getHibernateHelper().listContacts());

		app.getContactHelper().createContract(contact);

		// save new
		SortedListOf<ContactData> newstate = app.getContactHelper().getContacts();

		// check
		for (ContactData contactData : oldstate) {
			contactData.correctionContact();
		}
		assertThat(newstate, equalTo(oldstate.withAdded(contact.correctionContact())));
	}
	
}
